package com.ase.leetcode.翻转链表;

/**
 * @author gyhstart
 * @create 2021/3/18 - 19:08
 * <p>
 * 定义一个函数，输入一个链表的头节点，反转该链表并输出反转后链表的头节点。
 * 示例:
 * <p>
 * 输入: 1->2->3->4->5->NULL
 * 输出: 5->4->3->2->1->NULL
 **/
public class Solution {
    public static ListNode reverseList(ListNode head) {
        ListNode next;
        ListNode prev = null;
        ListNode curr = head;
        while (curr != null) {
            next = curr.next;
            curr.next = prev;
            prev = curr;
            curr = next;
        }
        return prev;
    }

    public static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            this.val = x;
        }
    }

    public static void getNodeList(ListNode node) {
        while (node != null) {
            System.out.print("node = " + node.val + "  ");
            node = node.next;
        }


    }

    public static void main(String[] args) {
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(3);
        ListNode node4 = new ListNode(4);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;

        getNodeList(node1);

        ListNode reversNode = reverseList(node1);
        System.out.println();
        getNodeList(reversNode);
    }
}


